package com.maila.calltaxi_driver.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.maila.calltaxi_driver.R;
import com.maila.calltaxi_driver.utils.RegularUtils;
import com.maila.calltaxi_driver.utils.StartActivityUtils;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/3.
 */

public class RegisterActivity extends AppCompatActivity {
    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.register_header)
    AutoRelativeLayout registerHeader;
    @BindView(R.id.enter_phone_number)
    EditText enterPhoneNumber;
    @BindView(R.id.delete_number_iv)
    ImageView deleteNumberIv;
    @BindView(R.id.all_zhuce)
    AutoLinearLayout allZhuce;
    @BindView(R.id.btn_register)
    Button btnRegister;
    @BindView(R.id.rb1)
    RadioButton rb1;
    @BindView(R.id.tv_rule)
    TextView tvRule;
    boolean isphone=false;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_activity);
        ButterKnife.bind(this);
        enterPhoneNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().length()>0&& RegularUtils.isPhoneNumber(s.toString())){
                    if(rb1.isChecked()){
                        btnRegister.setBackgroundResource(R.drawable.tv_bg_cz_gree);
                        btnRegister.setClickable(true);
                    }

                    isphone=true;
                }else {
                    if(s.length()==11&&!RegularUtils.isPhoneNumber(s.toString())){
                        Toast.makeText(RegisterActivity.this,"请输入正确的手机号",Toast.LENGTH_SHORT).show();
                    }
                    btnRegister.setBackgroundResource(R.drawable.tv_bg_cz_hui);
                    btnRegister.setClickable(false);
                    isphone=false;
                }
            }
        });
    }

    @OnClick({R.id.white_return_iv, R.id.delete_number_iv, R.id.btn_register, R.id.rb1, R.id.tv_rule})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.white_return_iv:
                finish();
                break;
            case R.id.delete_number_iv:
                enterPhoneNumber.setText("");
                break;
            case R.id.btn_register:
                StartActivityUtils.start(this,RegisterScendActivity.class,enterPhoneNumber.getText().toString());
                finish();
                break;
            case R.id.rb1:
                if(rb1.isChecked()&&isphone){
                    btnRegister.setBackgroundResource(R.drawable.tv_bg_cz_gree);
                    btnRegister.setClickable(true);
                }else {
                    btnRegister.setBackgroundResource(R.drawable.tv_bg_cz_hui);
                    btnRegister.setClickable(false);
                }
                break;
            case R.id.tv_rule:
                break;
        }
    }
}
